import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author Rob Powell
 */
public class javaexample extends Applet implements ActionListener {

//Load a menu with multiple buttons so the user can select which game they want to play
    Panel startPanel,prefsPanel,gamePanel;
    Button b1,b2,b3,b4,b5,submitPrefs;
    Choice c,c1,c2;

    @Override
    public void init() {

        //Set out a GridLayout mananger
        startPanel = new Panel();
        GridLayout layout = new GridLayout(5,1);
        startPanel.setLayout(layout);

        //Create the buttons and add the action listener
        b1 = new Button("Play an offline 2-player game. No login required");
        b1.addActionListener(this);

        //Add the buttons to the panel
        startPanel.add(b1);

        //Put the panel in the applet
        this.add(startPanel);
        
    }


    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            System.out.println("Here");
            this.remove(startPanel);
            //Show the panel for a two player local game
            prefsPanel = new Panel();
            BorderLayout b = new BorderLayout();
            prefsPanel.setLayout(b);

            //Create a new grid layout with game options
            Panel optionsPanel = new Panel();
            GridLayout options = new GridLayout(3,2);
            optionsPanel.setLayout(options);

            //Text box to ask for game type
            Label tb1 = new Label("Game Type:");
            optionsPanel.add(tb1);
            //Dropdown box with answers
            c = new Choice();
            c.add("101");
            c.add("201");
            c.add("301");
            c.add("501");
            c.add("701");
            c.add("1001");
            optionsPanel.add(c);

            //Text box to ask for number of legs per set
            Label tb2 = new Label("Best of how many legs?");
            optionsPanel.add(tb2);
            //Dropdown box with answers
            c1 = new Choice();
            c1.add("1");
            c1.add("3");
            c1.add("5");
            c1.add("7");
            c1.add("9");
            c1.add("11");
            optionsPanel.add(c1);

            //Text box to ask for number of sets
            Label tb3 = new Label("Best of how many sets?");
            optionsPanel.add(tb3);
            //Dropdown box with answers
            c2 = new Choice();
            c2.add("1");
            c2.add("3");
            c2.add("5");
            c2.add("7");
            optionsPanel.add(c2);

            //Add the options panel to the center of the main panel
            prefsPanel.add(optionsPanel,BorderLayout.CENTER);


            //Create the submit button
            submitPrefs = new Button("Submit");
            submitPrefs.addActionListener(this);
            prefsPanel.add(submitPrefs,BorderLayout.SOUTH);


            //Create a title label
            Label title = new Label("Please select your game settings:");
            prefsPanel.add(title,BorderLayout.NORTH);


            this.add(prefsPanel);
            this.validate();

        }

        else if (e.getSource() == submitPrefs) {
            //Get game prefs
            String game = c.getSelectedItem();
            String legs = c1.getSelectedItem();
            String sets = c2.getSelectedItem();

            int g = Integer.parseInt(game);
            int l = Integer.parseInt(legs);
            int s = Integer.parseInt(sets);

            System.out.println("This match will be the best of " + sets + " sets. Each set is the best of " + legs + " legs of " + game);

            //Create the game panel
            startGame(g,l,s);
        }
        else {
            System.out.println("Unknown event trigger");
        }
    }

    public void startGame(int game, int legs, int sets) {
        this.remove(prefsPanel);
        gamePanel = new Panel();
        BorderLayout b = new BorderLayout();
        gamePanel.setLayout(b);

        //Create a title label
        String s = "This match is the best of " + sets + " sets. Each set is the best of " + legs + " legs of " + game;
        Label title = new Label(s);
        gamePanel.add(title,BorderLayout.NORTH);

        updateGame(gamePanel);

        }

    public void updateGame(Panel gamePanel) {
        //Split the centre panel into another borderlayout
        Panel scorePanel = new Panel();
        BorderLayout bl = new BorderLayout();
        scorePanel.setLayout(bl);
        gamePanel.add(scorePanel,BorderLayout.CENTER);

        //North panel of b1 - Message with who to throw, box to enter score and a submit button
        Boolean p1 = true;
        String st = "";
        if (p1) {
            st = "Player 1 to throw";
        }
        else {
            st = "Player 2 to throw";
        }
        Label thr = new Label(st);
        scorePanel.add(thr,BorderLayout.NORTH);

        TextField tf = new TextField(30);
        scorePanel.add(tf,BorderLayout.NORTH);
        tf.addActionListener(this);
        tf.requestFocus();

        //West panel of b1 - checkout, 3-dart average and scores


        //East panel of b1 - checkout, 3-dart average and scores


        this.add(gamePanel);
        this.validate();
    }

}